package com.lxd.controller;

import java.util.Date;
import java.util.List;

import com.lxd.entity.NewsRequest;
import com.lxd.entity.query.NewsQuery;
import com.lxd.entity.po.News;
import com.lxd.entity.vo.NewsVO;
import com.lxd.entity.vo.PaginationResultVO;
import com.lxd.entity.vo.ResponseVO;
import com.lxd.service.NewsService;
import com.lxd.utils.CopyTools;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 新闻表 Controller
 */
@RestController("newsController")
@RequestMapping("/news")
public class NewsController extends ABaseController{

	@Resource
	private NewsService newsService;
	/**
	 * 根据条件分页查询
	 */
	@RequestMapping("/loadDataList")
	public ResponseVO loadDataList(NewsQuery query){
		PaginationResultVO<NewsVO> listByPage = newsService.findListByPage(query);
		return getSuccessResponseVO(listByPage);
	}

	/**
	 * 管理员查询新闻列表
	 */
	@RequestMapping("/admin/loadDataList")
	public ResponseVO adminLoadDataList(NewsQuery query) {
		return getSuccessResponseVO(newsService.findListByPage(query));
	}

	/**
	 * 新增
	 */
	@RequestMapping("/add")
	public ResponseVO add(News bean) {
		newsService.add(bean);
		return getSuccessResponseVO(null);
	}
	/**
	 * 管理员添加新闻
	 */
	@RequestMapping("/admin/add")
	public ResponseVO adminAdd(@RequestBody NewsRequest newsRequest) {
		newsService.Adminadd(newsRequest);
		return getSuccessResponseVO(null);
	}


	/**
	 * 批量新增
	 */
	@RequestMapping("/addBatch")
	public ResponseVO addBatch(@RequestBody List<News> listBean) {
		newsService.addBatch(listBean);
		return getSuccessResponseVO(null);
	}

	/**
	 * 批量新增/修改
	 */
	@RequestMapping("/addOrUpdateBatch")
	public ResponseVO addOrUpdateBatch(@RequestBody List<News> listBean) {
		newsService.addBatch(listBean);
		return getSuccessResponseVO(null);
	}

	/**
	 * 根据Id查询对象
	 */
	@RequestMapping("/getNewsById")
	public ResponseVO getNewsById(Long id) {
		return getSuccessResponseVO(newsService.getNewsById(id));
	}

	/**
	 * 管理员设置/取消热门
	 */
	@RequestMapping("/admin/toggleHot")
	public ResponseVO toggleHot(@RequestBody NewsRequest newsRequest) {
		News news = newsService.getNewsById(newsRequest.getId());
		if (news != null) {
			NewsRequest updateRequest = new NewsRequest();
			updateRequest.setId(news.getId());
			updateRequest.setHot(news.getHot() == 1 ? 0 : 1);
			updateRequest.setUpdatedAt(new Date());
			newsService.updateNewsById(updateRequest);
		}
		return getSuccessResponseVO(null);
	}

	/**
	 * 根据Id修改对象
	 */
	@RequestMapping("/updateNewsById")
	public ResponseVO updateNews(@RequestBody NewsRequest newsRequest) {
		newsRequest.setUpdatedAt(new Date());
		newsService.updateNewsById(newsRequest);
		return getSuccessResponseVO(null);
	}

	/**
	 * 根据Id删除
	 */
	@RequestMapping("/deleteNewsById")
	public ResponseVO deleteNewsById(@RequestBody NewsRequest newsRequest) {
		newsService.deleteNewsById(newsRequest.getId());
		return getSuccessResponseVO(null);
	}

	@RequestMapping("/totalCount")
	public ResponseVO totalCount() {
		Integer count = newsService.getTotalCount();
		return getSuccessResponseVO(count);
	}
}